import { ref, reactive, toRefs } from 'vue'
import request from '@/utils/request.js'

// ======================================
// 1. 请求参数类型定义（严格 TypeScript 约束）
// ======================================
// 创建表的请求
interface CreateTableSqlRequest {
    sql: string,
    databaseName: string;
}

// 创建列的请求
interface CreateTableColumnSql {
    sql: string,
    databaseName: string;
}
//生成sql
interface GenerateSqlRequest{
    databaseName : string;
}

// ======================================
// 2. 响应结果类型（泛型增强 + 语义化）
// ======================================
interface BaseResponse<T = any> {
    code?: number
    message?: string
    data: {
        data: T;
        timestamp: number;
    };
}

// ======================================
// 3. 通用请求工具（消除重复逻辑）
// ======================================
const baseRequest = async <T>(
    state: {
        loading: boolean
        error: string | null
        response: BaseResponse<T> | null
    },
    url: string,
    method: 'GET' | 'POST',
    params?: Record<string, any>, // GET 参数
    data?: Record<string, any>    // POST 请求体
): Promise<BaseResponse<T>> => {
    state.loading = true
    state.error = null
    state.response = null

    try {
        const response = await request({ url, method, params, data })
        state.response = response as BaseResponse<T>
        return response
    } catch (error: any) {
        state.error = error.message || '请求失败'
        throw error // 抛出错误供上层处理
    } finally {
        state.loading = false
    }
}

// ======================================
// 4. 业务方法 & 状态管理（清晰隔离）
// ======================================
export const useTableSqlService = () => {
    // 每个方法独立管理状态，避免混淆
    const createTableSqlState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<null> | null,
    })

    // 创建列的状态
    const createTableColumnSqlState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<null> | null,
    })
    //生成sql的状态
    const generateSqlState = reactive({
        loading: false,
        error: null as string | null,
        response: null as BaseResponse<string> | null,
    })

    // 创建表 - 将参数作为Query参数传递（第三个参数）
    const createTableSql = async (params: CreateTableSqlRequest) => {
        return baseRequest<null>(
            createTableSqlState,
            '/database/sql/parseCreateTableName',
            'POST',
            params, // 这里修改为params而不是data
            undefined
        )
    }

    // 创建列 - 将参数作为Query参数传递
    const createTableColumnSql = async (params: CreateTableColumnSql) => {
        return baseRequest<null>(
            createTableColumnSqlState,
            '/database/sql/parseGetColumnsList',
            'POST',
            params, // 这里修改为params而不是data
            undefined
        )

    }
    //生成sql
    const generateSql =async (params: GenerateSqlRequest) => {
        return baseRequest<string>(
            generateSqlState,
            '/database/sql/generateSql',
            'GET',
            params,
            undefined
        )
    }

    // ======================================
    // 5. 导出响应式状态 & 方法
    // ======================================
    return {
        ...toRefs(createTableSqlState),
        ...toRefs(createTableSqlState),
        ...toRefs(createTableColumnSqlState),
        createTableSql,
        createTableColumnSql,
        generateSql
    }
}
